package com.hdy.javachatroomdemo.Service;

import com.hdy.javachatroomdemo.Model.Friend;
import com.hdy.javachatroomdemo.Model.Session;
import com.hdy.javachatroomdemo.Model.SessionMapper;
import com.hdy.javachatroomdemo.Model.SessionUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SessionService {
    @Autowired
    private SessionMapper sessionMapper;
    public List<Integer> getSessionByUserId(int userId) {
        List<Integer> sessionIdList =sessionMapper.getSessionByUserId(userId);
        //这里会话列表为空不需要做任何特殊处理
        return sessionIdList;
    }
    public List<Friend> getFriendBySessionId(int sessionId ,int selfUserId) {
        List<Friend> friendList =sessionMapper.getFriendBySessionId(sessionId,selfUserId);
        //这里会话中的好友列表为空不需要做任何特殊处理
        return friendList;
    }

    public Map<String,Integer> addSession(int userId, int friendId) {
        Map<String,Integer> map=new HashMap<>();
        Session session =new Session();
        //插入session
        sessionMapper.addSession(session);
        //插入好友_session数据
        SessionUser item1 =new SessionUser();
        item1.setSessionId(session.getSessionId());
        item1.setUserId(userId);
        sessionMapper.addSessionUser(item1);
        SessionUser item2 =new SessionUser();
        item2.setSessionId(session.getSessionId());
        item2.setUserId(friendId);
        sessionMapper.addSessionUser(item2);
        System.out.println("[SessionService:addSession]新增会话成功,sessionId = "+session.getSessionId()
        +",userId = "+userId+",friendId = "+friendId);
        map.put("sessionId",(int)session.getSessionId());
        return map;
    }
}
